__author__ = 'Administrator'
from pages.BasePage import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class MainPageLocators(object):
    NEW_MESSAGE_BUTTON = (By.ID, "newmsgc")
    MAIL_LIST = (By.ID, "divScrollbar")

    UNREAD_MAIL_MENU = (By.XPATH, "//*[@fldrnm=\"Unread Mail\"]")
    INBOX_MAIL_MENU = (By.XPATH, "//*[@fldrnm=\"Inbox\"]")
    DELETED_MAIL_MENU = (By.XPATH, "//*[@fldrnm=\"Deleted Items\"]")

    MOVE_MENU_BUTTON = (By.ID, "divToolbarButtonmove")
    DELETE_MENU_BUTTON = (By.ID, "divToolbarButtondelete")
    FAVORITES = (By.ID, "spnTrNdCnt")

    SUBJECT_LABEL = (By.ID, "divConvTopic")
    BODY_LABEL = (By.XPATH, "//*[@id=\"divBdy\"]/div/div/div/font/span")
    RECIPIENT_LABEL = (By.ID, "spnR")


class MainPage(BasePage):
    
    def __init__(self, driver):
        super(MainPage, self).__init__(driver)
        self.pageLocation = "https://win-m4n3psr54be.contoso.local/owa"

    def click_new_message_button(self):
        self.find_element(MainPageLocators.NEW_MESSAGE_BUTTON).click()

    def click_unread_mail_menu(self):
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.find_element(MainPageLocators.FAVORITES, 2).click()
        self.find_element(MainPageLocators.UNREAD_MAIL_MENU).click()

    def click_inbox_mail_menu(self):
        self.find_element(MainPageLocators.FAVORITES, 2).click()
        self.find_element(MainPageLocators.INBOX_MAIL_MENU).click()

    def click_deleted_mail_menu(self):
        self.find_element(MainPageLocators.DELETED_MAIL_MENU).click()

    def go_to_last_unread_mail(self):
        self.find_element(MainPageLocators.MAIL_LIST, 1).click()
        self.find_element(MainPageLocators.MAIL_LIST, 1).send_keys(Keys.HOME)

    def go_to_last_inbox_mail(self):
        self.find_element(MainPageLocators.MAIL_LIST, 0).click()
        self.find_element(MainPageLocators.MAIL_LIST, 0).send_keys(Keys.HOME)

    def click_delete_menu_button(self):
        self.find_element(MainPageLocators.DELETE_MENU_BUTTON, 3).click()

    def click_move_menu_button(self):
        self.find_element(MainPageLocators.MOVE_MENU_BUTTON, 2).click()

    def get_email_info(self):
        body = self.find_element(MainPageLocators.BODY_LABEL)
        recipient = self.find_element(MainPageLocators.RECIPIENT_LABEL)
        subject = self.find_element(MainPageLocators.SUBJECT_LABEL)

        return body.text, recipient.text, subject.text